package designPatterns.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 实现 jkd提供的 InvocationHandler 接口，重新invoke方法
 *  引入目标对象 target，第二步中的对象
 */
public class CalculatorInvocationHandler implements InvocationHandler {
    private final Object target;
    public CalculatorInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before " + method.getName() + " operation");
        Object result = method.invoke(target, args);
        System.out.println("after " + method.getName() + " operation");
        return result;
    }
}